package com.example.easyscript.service.task;

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.easyscript.bean.task.EntranceBean;
import com.example.easyscript.entity.task.Entrance;
import com.example.easyscript.mapper.EntranceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author huzixian
* @date  2023-03-08
*/
@Slf4j
@Service
public class EntranceService extends ServiceImpl<EntranceMapper, Entrance>{

    @Resource
    private ImageService imageService;

    public Map<Integer, EntranceBean> getEntranceBeanMapByTaskId(Integer taskId) {
        List<Entrance> entranceList = TaskLoader.loadEntityListByTaskId(taskId, this, Entrance.class);
        Map<Integer, EntranceBean> entranceMap = entranceList.stream().map(item -> {
            EntranceBean entranceBean = CglibUtil.copy(item, EntranceBean.class);
            String url = imageService.getUrlByImageId(entranceBean.getImageId());
            entranceBean.setPictureUrl(url);
            return entranceBean;
        }).collect(Collectors.toMap(EntranceBean::getOrderId, item -> item,(a,b)->a));
        return entranceMap;
    }
}
